package servlet.ann.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Map;

@MultipartConfig
@WebServlet(urlPatterns = "/ps")
public class ParamServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("ParamServlet-doGet");
        Map<String, String[]> parameterMap = req.getParameterMap();
        PrintWriter writer = resp.getWriter();
        parameterMap.forEach((k, v) -> {
            System.out.println(k + ":" + v[0]);
            writer.println(k + ":" + v[0]);
        });
        writer.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("ParamServlet-doPost");

        Map<String, String[]> parameterMap = req.getParameterMap();
        PrintWriter writer = resp.getWriter();
        parameterMap.forEach((k, v) -> {
            System.out.println(k + ":" + v[0]);
            writer.println(k + ":" + v[0]);
        });
        writer.close();
        System.out.println("===========================================");
        // 文件上传
        Collection<Part> parts = req.getParts();
        parts.parallelStream().forEach((x) -> {
            System.out.println(x.getName());
        });
        Part myfile = req.getPart("myfile");
        // myfile.write("D:/123.txt");
        System.out.println(myfile.getHeader("Content-Disposition"));
    }
}